<template>
    <div class="login-wrap">
        <header class="login-header">
            <h1>大华医卫后台管理系统</h1>
        </header>
        <div class="ms-login">
            <div class="ms-title">欢迎登录</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username" prefix-icon="el-icon-user" style="width: 320px">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="param.password"
                        @keyup.enter="submitForm()"
                        :style="{width: 320}"
                        prefix-icon="el-icon-lock"
                        style="width: 320px"
                    >
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()" style="width: 320px">登录</el-button>
                </div>
                <p class="login-tips">Tips : 用户名和密码随便填。</p>
            </el-form>
        </div>
    </div>
</template>

<script>
import { login } from "@/api/login";
export default {
    data() {
        return {
            param: {
                username: "",
                password: ""
            },
            rules: {
                username: [
                    { required: true, message: "请输入用户名", trigger: "blur" }
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.$store.commit("clearTags");
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(async (valid) => {
                if (valid) {
                    const res = await login(this.param);
                    if(res.code === 200){
                        this.$message.success("登录成功，2s后自动跳转页面");
                        localStorage.setItem("token", res?.token);
                        localStorage.setItem("user", JSON.stringify(res?.result))
                        setTimeout(() => {
                            this.$router.push("/");
                        }, 2000)
                        return
                    }
                    this.$message.error(res.error || "登录失败");
                } else {
                    this.$message.error("请输入账号和密码");
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/login-bg.png);
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: center;
}
.login-header {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    height: 80px;
    background-color: hsla(0,0%,100%,.2);
    text-align: center;
    line-height: 80px;
}
.login-header h1 {
    letter-spacing: 1px;
    font-size: 32px;
    margin: 0;
    margin-left: 20px;
    font-weight: 700;
    color: #fffffd;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    font-weight: 500;
    color: #333;
}
.ms-login {
    width: 400px;
    height: 320px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    background: #FFF;
    overflow: hidden;
}
.ms-content {
    padding: 10px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>